package com.zenchn.service.impl;

import com.zenchn.dao.TSysAccountMapper;
import com.zenchn.dao.TSysAccountRoleMapperMapper;
import com.zenchn.dynamic.TSysAccountRoleMapperDynamicMapper;
import com.zenchn.enums.CommonStatusEnum;
import com.zenchn.generic.GenericDao;
import com.zenchn.generic.GenericServiceImpl;
import com.zenchn.model.*;
import com.zenchn.model.TSysAccountRoleMapper;
import com.zenchn.service.AccountRoleMapperService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * AccountRoleMapperServiceImpl：
 * 系统账户 角色映射实现类
 * @author dingxinfa
 * @date 2020-04-16 11:46
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AccountRoleMapperServiceImpl extends GenericServiceImpl<TSysAccountRoleMapper,String, TSysAccountRoleMapperExample> implements AccountRoleMapperService {


    @Resource
    private TSysAccountRoleMapperMapper accountRoleMapperMapper;

    @Resource
    private TSysAccountMapper accountMapper;

    @Resource
    private TSysAccountRoleMapperDynamicMapper accountRoleMapperDynamicMapper;

    @Override
    public GenericDao<TSysAccountRoleMapper, String, TSysAccountRoleMapperExample> getDao() {
        return accountRoleMapperMapper;
    }

    @Override
    public List<TSysAccount> selectAccountByRoleIds(List<String> roleIds) {


        TSysAccountRoleMapperExample accountRoleMapperExample = new TSysAccountRoleMapperExample();
        accountRoleMapperExample.createCriteria().andRoleIdIn(roleIds);
        List<TSysAccountRoleMapper> accountRoleMapperList=accountRoleMapperMapper.selectByExample(accountRoleMapperExample);

        List<String> accountIds=accountRoleMapperList.stream().map(TSysAccountRoleMapper::getAccountId).distinct().collect(Collectors.toList());
        if (!accountIds.isEmpty()) {
            TSysAccountExample accountExample = new TSysAccountExample();
            accountExample.createCriteria().andAccountIdIn(accountIds).andStatusGreaterThanOrEqualTo(CommonStatusEnum.DISABLE.getCode());
            return accountMapper.selectByExample(accountExample);
        }
        return null;
    }
}
